package by.grsu.shedule.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * The Class DBHelper.
 */
public class DBImportantLessonHelper extends SQLiteOpenHelper {

	public static final String IMPORTANT_LESSON_TABLE = "IMPORTANT_LESSON";

	public static final String[] TABLE_COLUMNS = { "ID", "SUBJECT", "TEACHER",
			"GRUPA", "COURSE", "LANGUAGE" };

	public static final int ID_COL_INDEX = 0;

	public static final int SUBJECT_COL_INDEX = 1;

	public static final int TEACHER_COL_INDEX = 2;

	public static final int GROUP_COL_INDEX = 3;

	public static final int COURSE_COL_INDEX = 4;

	public static final int LANGUAGE_COL_INDEX = 5;

	/** The Constant CREATE_TABLE_SQL. */
	public static final String CREATE_FILES_TABLE_SQL = "CREATE TABLE "
			+ IMPORTANT_LESSON_TABLE + " (" + TABLE_COLUMNS[ID_COL_INDEX]
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ TABLE_COLUMNS[SUBJECT_COL_INDEX] + " VARCHAR(150), "
			+ TABLE_COLUMNS[TEACHER_COL_INDEX] + " VARCHAR (100), "
			+ TABLE_COLUMNS[GROUP_COL_INDEX] + " VARCHAR (50), "
			+ TABLE_COLUMNS[COURSE_COL_INDEX] + " INTEGER, "
			+ TABLE_COLUMNS[LANGUAGE_COL_INDEX] + " VARCHAR (50))";

	/** The Constant DROP_TABLE_SQL. */
	public static final String DROP_FILES_TABLE_SQL = "DROP TABLE IF EXISTS "
			+ IMPORTANT_LESSON_TABLE;
	public static final String SUBJECT_SELECTION = "SUBJECT=";

	public static final String TEACHER_SELECTION = "TEACHER=";

	public static final String GROUP_SELECTION = "GRUPA=";

	public static final String COURSE_SELECTION = "COURSE=";

	public static final String LANGUAGE_SELECTION = "LANGUAGE=";

	/**
	 * Instantiates a new dB helper.
	 * 
	 * @param context
	 *            the context
	 * @param name
	 *            the name
	 * @param factory
	 *            the factory
	 * @param version
	 *            the version
	 */
	public DBImportantLessonHelper(final Context context, final String name,
			final CursorFactory factory, final int version) {
		super(context, name, factory, version);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
	 * .SQLiteDatabase)
	 */
	@Override
	public final void onCreate(final SQLiteDatabase db) {
		try {
			db.beginTransaction();
			db.execSQL(CREATE_FILES_TABLE_SQL);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
	 * .SQLiteDatabase, int, int)
	 */
	@Override
	public final void onUpgrade(final SQLiteDatabase db, final int oldVersion,
			final int newVersion) {
		try {
			db.beginTransaction();
			db.execSQL(DROP_FILES_TABLE_SQL);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
		onCreate(db);
	}

}
